package appBord

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject

class AppBordInfoController {

    def index() {
        render AppBordInfo.list() as JSON
    }

    def show(){
        def alamInstance
        if(params?.id){
            alamInstance = AppBordInfo.get(Long.parseLong(params.id))
        }else{
            alamInstance = AppBordInfo.newInstance()
        }

        render(view: "/appBord/appBordInfo/update.gsp",model: [alamInstance:alamInstance])
    }

    def update(){
        def resultJson = JSONObject.newInstance()
        resultJson.put("statusCode",200)
        resultJson.put("message","操作成功")
        try {
            def id = params?.id
            def appBordAlarm
            if(id){
                appBordAlarm = AppBordInfo.get(id)
            }else{
                appBordAlarm = AppBordInfo.newInstance()
            }

            appBordAlarm.name = params?.name
            appBordAlarm.nameDesc = params?.nameDesc
            appBordAlarm.dataSql = params?.dataSql
            appBordAlarm.save(flush: true)
        }catch (Exception e){
            resultJson.put("statusCode",-200)
            resultJson.put("message","Error:${e.getMessage()}")
            e.printStackTrace()
        }finally{
            render resultJson.toString()
        }
    }

    def delete(){
        def resultJson = JSONObject.newInstance()
        resultJson.put("statusCode",200)
        resultJson.put("message","操作成功")

        try {
            def id = Long.parseLong(params?.id)
            def  alamInstance = AppBordInfo.get(id)
            alamInstance.delete(flush: true)
            AppBordNodeMonitor.findAllByBoardId(id)?.each {
                it.delete(flush: true)
            }
        }catch (Exception e){
            resultJson.put("statusCode",-200)
            resultJson.put("message","Error:${e.getMessage()}")
            e.printStackTrace()
        }finally{
            render resultJson.toString()
        }
    }
}
